home *** CD-ROM | disk | FTP | other *** search
- Path: ub239.dialup.uwa.edu.au!localhost!prye
- From: prye@cyllene.uwa.edu.au (Peter Rye)
- Newsgroups: comp.lang.c++
- Subject: Re: another overloading question
- Date: 04 Apr 1996 17:12:01 GMT
- Organization: The University of Western Australia
- Distribution: world
- Message-ID: <PRYE.96Apr5011201@cyllene.uwa.edu.au>
- References: <4ju98d$3al@panoramix.fi.upm.es> <4jvst5$snq@grimsel.zurich.ibm.com>
- NNTP-Posting-Host: ub239.dialup.uwa.edu.au
- In-reply-to: wgk@zurich.ibm.com's message of 4 Apr 1996 07:14:45 GMT
- X-Mailer: GNU Emacs 19.28
-
- >>>>> "Keith" == Keith Whittingham <wgk@zurich.ibm.com> writes:
-
- Keith> In <4ju98d$3al@panoramix.fi.upm.es>, sacha@diafi.upm.es
- Keith> (Sacha) writes:
-
- >> Can I overload = for simple types?
-
- >> class Colour {
- >> private:
- >> float r_, g_, b_;
- >> public:
- >> Colour(float r, float g, float b) {r_=r; g_=g; b_=b;}
-
- Keith> ++++ int operator=(void)
- Keith> ++++ { return (r_ + g_ + b_) / 3;}
-
- >> };
- >>
- >> is there some way I can overload = so that I can do things
- >> like:
- >>
- >> Colour colour(0.99238, 0.172364, 0.273);
- >>
- >> int intensity = colour;
- >>
-
- Keith> That should do the trick
-
- I don't think so.
- My compiler complains:
-
- ub239:~$ g++ -o check check.cc
- check.cc:11: `Colour::operator =()' must take exactly one argument
- check.cc:16: `class Colour' used where a `int' was expected
-
- "Operator overloading cannot modify the rules for operators applied
- to types for which they are defined by the language itself." -- ARM
-
- You perhaps meant:
- operator int() { return ......}
- --
- Peter Rye prye@cyllene.uwa.edu.au, prye@ichr.uwa.edu.au
- Respiratory Research Fellow, Princess Margaret Hospital for Children
- Perth, Western Australia Ph: +61 (09) 340 8985, Fax: +61 (09) 388 2097
- ** Smoking areas in restaurants are like peeing areas in swimming pools. **
-